Conditions | 16 |
Paths | 144 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like get-main.js ➔ renderAbeAdmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import path from 'path' |
||
19 | function renderAbeAdmin(EditorVariables, obj, filePath) { |
||
20 | var manager = {} |
||
21 | |||
22 | manager.home = { |
||
23 | files: []//Manager.instance.getList() |
||
24 | } |
||
25 | |||
26 | manager.list = Manager.instance.getStructureAndTemplates() |
||
27 | manager.editConfig = EditorVariables.express.req.app.get('config') |
||
28 | manager.config = JSON.stringify(config) |
||
29 | |||
30 | var _hasBlock = (obj) ? obj.hasBlock : false |
||
31 | var _hasSingleBlock = (obj) ? obj.hasSingleBlock : false |
||
32 | var _preview = (filePath) ? '/abe/page/' + EditorVariables.express.req.params[0] + `?filePath=${EditorVariables.express.req.query.filePath}` : false |
||
33 | var _form = (obj) ? obj.form : false |
||
34 | var _json = (obj) ? obj.json : false |
||
35 | var _filePath = (filePath) ? filePath : false |
||
36 | if (_filePath) { |
||
37 | _filePath = '/' + _filePath.replace(/^\/+/, '') |
||
|
|||
38 | } |
||
39 | |||
40 | var pageHtml = '' |
||
41 | if(typeof _json !== 'undefined' && _json !== null && typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) { |
||
42 | |||
43 | var text = cmsTemplates.template.getTemplate(_json.abe_meta.template, _json) |
||
44 | var page = new Page(_json.abe_meta.template, text, _json, false) |
||
45 | pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>') |
||
46 | } |
||
47 | |||
48 | var editorWidth = '33%' |
||
49 | EditorVariables.express.req.headers && EditorVariables.express.req.headers.cookie && EditorVariables.express.req.headers.cookie.split(';').forEach(function(cookie) { |
||
50 | var parts = cookie.match(/(.*?)=(.*)$/) |
||
51 | if ( typeof parts !== 'undefined' && parts !== null && ( parts.length > 2 ) && ( parts[1] === 'editorWidth' ) ) editorWidth = parts[2] |
||
52 | }) |
||
53 | |||
54 | EditorVariables.pageHtml = pageHtml |
||
55 | EditorVariables.test = JSON.stringify(locale) |
||
56 | EditorVariables.text = locale |
||
57 | EditorVariables.preview = _preview |
||
58 | EditorVariables.hasSingleBlock = _hasSingleBlock |
||
59 | EditorVariables.hasBlock = _hasBlock |
||
60 | EditorVariables.form = _form |
||
61 | EditorVariables.json = _json |
||
62 | EditorVariables.manager = manager |
||
63 | // EditorVariables.nonce = '\'nonce-' + EditorVariables.express.res.locals.nonce + '\'' |
||
64 | EditorVariables.editorWidth = editorWidth |
||
65 | |||
66 | if (_json != null && _json.abe_meta) { |
||
67 | EditorVariables.workflows = User.utils.getUserWorkflow(_json.abe_meta.status) |
||
68 | } |
||
69 | |||
70 | EditorVariables = abeExtend.hooks.instance.trigger('afterVariables', EditorVariables) |
||
71 | |||
72 | if (filePath != null && filePath.indexOf('.json') > -1) { |
||
73 | EditorVariables.express.res.set('Content-Type', 'application/json') |
||
74 | EditorVariables.express.res.send(JSON.stringify(_json)) |
||
75 | }else { |
||
76 | EditorVariables.express.res.render(config.abeEngine, EditorVariables) |
||
77 | } |
||
78 | } |
||
79 | |||
177 |